package com.qingxin.wuye.repertory.action;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.wuye.repertory.entity.ResType;
import com.qingxin.wuye.repertory.service.ResTypeService;

@Controller
@RequestMapping("/restype")
public class ResTypeAction extends BaseAction{
	@Autowired
	private ResTypeService resTypeService;
	
	
	@RequestMapping(value="/getTypeData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getTypeData(HttpServletRequest request){
		return resTypeService.getUITreeData(getLoginUser(request), false);
	}
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String index(ModelMap model , HttpServletRequest request) {
		return "wuye/restype/restype_list";
	}
	
	
	/**
	 * 列表数据
	 * @param searchname
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String searchname, PageParam pageParam, HttpServletRequest request){
		return resTypeService.list(searchname, pageParam, getLoginUser(request));
	}
	
	
	
	/**
	 * 添加类别信息
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "wuye/restype/restype_add";
	}
	
	
	/**
	 * 保存类别信息
	 * @param resType
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid ResType resType, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = resTypeService.add(resType, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改类别信息
	 * @param model
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String typeid, HttpServletRequest request){
		ResType resType = resTypeService.get(typeid);
		model.put("restype", resType);
		return "wuye/restype/restype_edit";
	}
	
	
	/**
	 * 保存修改
	 * @param resType
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid ResType resType, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = resTypeService.update(resType, getLoginUser(request), request);
		return info;
	}
	
	
	
	/**
	 * 删除
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] typeid, HttpServletRequest request){
		if(typeid==null || typeid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return resTypeService.delete(typeid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断类别名称是否存在
	 * @param typeName
	 * @param typeid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String typeName, String typeid){
		boolean existed = resTypeService.isNameDuplicate(typeName, typeid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断类别代码是否被使用
	 * @param typeCode
	 * @param typeid
	 * @return
	 */
	@RequestMapping(value="/isCodeDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isCodeDuplicate(String typeCode, String typeid){
		boolean existed = resTypeService.isCodeDuplicate(typeCode, typeid);
		return existed ? "1":"0";
	}
	
}
